package com.szqhyxj.youxiangju.biz.card.addCard;

import android.support.annotation.NonNull;

import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.CardInfo;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.RetrofitFacade;

import java.io.File;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class BindCardModel{

    public interface BindCardService {
        ///user/bankCard/edit
        @Multipart
        @POST("ocr/bankCard")
        Observable<BaseResult<CardInfo>> ocrBankCard(@Part MultipartBody.Part file, @Part("token") RequestBody token);

        @FormUrlEncoded
        @POST("msg/sms/addbankcardsend")
        Observable<BaseResult> addBankCardSend(@Field("phone") String phone);


        @FormUrlEncoded
        @POST("user/bankCard/add")
        Observable<BaseResult> addBankCard(@Field("btName") String btName, @Field("username") String username, @Field("bankCardNo") String bankCardNo, @Field("cardPhone") String cardPhone, @Field("bankCardPic") String bankCardPic, @Field("cvn") String cvn, @Field("expiryDate") String expiryDate, @Field("dueDate") String dueDate, @Field("billingDate") String billingDate, @Field("fixedAmount") String fixedAmount, @Field("sendValidCode") String sendValidCode, @Field("btCode") String btCode);
    }


    public static Observable<BaseResult<CardInfo>> bankCreditCard (File file) {
        MultipartBody.Part filePart = MultipartBody.Part.createFormData(
                "card",
                file.getName(),
                RequestBody.create(MediaType.parse("image/*"), file)
        );
        return RetrofitFacade.Companion.getUploadRetrofit().create(BindCardService.class).ocrBankCard(filePart,getToken());
    }

    public static  Observable<BaseResult> addBankCardSend (String phone) {
        return RetrofitFacade.Companion.getRetrofit().create(BindCardService.class).addBankCardSend(phone);
    }

    public static  Observable<BaseResult> addBankCard (BindCardEntity bindCardEntity) {
        return RetrofitFacade.Companion.getRetrofit().create(BindCardService.class)
                .addBankCard(bindCardEntity.getBtName(),
                        bindCardEntity.getUsername(),
                        bindCardEntity.getBankCardNo(),
                        bindCardEntity.getCardPhone(),
                        bindCardEntity.getBankCardPic(),
                        bindCardEntity.getCvn(),
                        bindCardEntity.getExpiryDate(),
                        bindCardEntity.getDueDate(),
                        bindCardEntity.getBillingDate(),
                        bindCardEntity.getFixedAmount(),
                        bindCardEntity.getSendValidCode(),
                        bindCardEntity.getBtCode());
    }
    @NonNull
    private static RequestBody getToken() {
        return RequestBody.create(MediaType.parse("text/plain"), TokenManager.INSTANCE.getMToken());
    }

}
